
module.exports = (app, router) => {
    const articleType = require('../../models/articleType')
    const { requestResult, pagingResult } = require('../../plugins/index')
    const count = require('../../models/counts')

    router.get('/findArticleType', async (req, res) => {
        const page = req.query.page || 1
        const pageSize = req.query.pageSize || 10
        const data = await pagingResult(articleType, page, pageSize)
        res.send(requestResult(data))
    })

    router.post('/addArticleType', async (req, res) => {
        //文章类型id自增
        const typeInfo = await count.findOneAndUpdate({
            name: 'articleTypeId'
        },{
            $inc: { 'id': 1}
        },{
            new: true
        })
        if(typeInfo){
            req.body.id = typeInfo.id
        }else{
            const info = {
                name: 'articleTypeId',
                id: 1
            }
            const i = await count.create(info)
            console.log(i);
            req.body.id = i.id
        }
        console.log(req)
        const data = await articleType.create(req.body)
        res.send(requestResult(data))
    })

    router.delete('/deleteArticleType/:id', async (req, res) => {
        await articleType.findOneAndRemove({ 'id': req.params.id }, (err, item) => {
            res.send(requestResult(item))
        })
        
    })

    router.post('/editArticleType', async (req, res) => {
        console.log(req.body);
        await articleType.findOneAndUpdate({ 'id': req.body.id}, req.body, (err, item) => {
            res.send(requestResult(item))
        })
    })

    app.use('/admin/api', router)
}